﻿using Jayrock.Json;
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;
//object对象序列化成json
namespace open_api_sdk_csharp.com.chinaums.open.api.internals.utils
{
    public class OpenModelParser
    {
        public JsonObject serializeRequestObject(Object res)
        {
            PropertyInfo[] pis = res.GetType().GetProperties();
            JsonObject jo = new JsonObject();
            foreach (PropertyInfo pi in pis)
            {
                if (!pi.CanRead)
                {
                    continue;
                }
                String elementName = pi.Name;
                Object value = getPiValue(res, pi);
                if (!String.IsNullOrEmpty(elementName))
                {
                    Object serialized = serializeValue(value);
                    if (serialized != null)
                    {
                        jo.Put(elementName, serialized);
                    }
                }
            }
            return jo;
        }

        private Object serializeValue(Object value)
        {
            if (value == null)
            {
                return null;
            }

            Object result = null;
            if (value is IDictionary)
            {
                result = serializeDicValue((IDictionary)value);
            }
            else if (value is ICollection)
            {
                result = serializeArrayValue((ICollection)value);
            }
            else
            {
                result = serializeElementValue(value);
            }
            return result;
        }

        private Object serializeElementValue(Object value)
        {
            if (value == null)
            {
                return null;
            }

            Object result = null;
            if (value is OpenApiObject)
            {
                result = serializeRequestObject((OpenApiObject)value);
            }else
            {
                result = value;
            }
            return result;
        }

        private Object serializeDicValue(IDictionary dic)
        {
            if (dic == null)
            {
                return null;
            }

            JsonObject jo = new JsonObject();
            foreach (String key in dic.Keys)
            {
                Object dicValue = dic[key];
                Object serializedValue = serializeValue(dicValue);
                jo.Put(key, serializedValue);
            }
            return jo;
        }

        private Object serializeArrayValue(ICollection collection)
        {
            if (collection == null)
            {
                return null;
            }
            JsonArray ja = new JsonArray();
            foreach (var item in collection)
            {
                ja.Add(serializeValue(item));
            }
            return ja;
        }

        private Object getPiValue(Object res, PropertyInfo pi)
        {
            if (!pi.CanRead)
            {
                return null;
            }
            Object value = pi.GetValue(res, null);
            return value;
        }

        private  T parse<T>(string jsonString)
        {
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
            {
                return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
            }
        }
    }
}
